include(CheckCXXCompilerFlag)

# Check for C++14 support
function(CHECK_CXX14)
  check_cxx_compiler_flag("-std=c++14" support_14)

  if(support_14)
    set(CXX14_FLAG "-std=c++14" PARENT_SCOPE)
  else()
    message(FATAL_ERROR "Compiler ${CMAKE_CXX_COMPILER} does not support C++14 standard")
  endif()
  set(CMAKE_CXX_FLAGS ${CXX14_FLAG} PARENT_SCOPE)
endfunction()

set(GXX_ADDITIONAL_FLAGS "-O2 -g -ggdb -fno-strict-aliasing -fstack-protector-strong -Wall -D_FORTIFY_SOURCE=2")	#  -fPIC is added differently, -DPIC is unknown by g++
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GXX_ADDITIONAL_FLAGS}")

# GCC has this options, clang doesn't
check_cxx_compiler_flag("-Wformat-security" COMPILER_HAS_WARNING_FORMAT_SECURITY)
if(COMPILER_HAS_WARNING_FORMAT_SECURITY)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=format-security")
endif()

#
# Nice options, maybe for future use
#
#if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
#  check_cxx14()
#  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Wconversion -Wshadow")
#
#  check_cxx_compiler_flag("-Wpedantic" COMPILER_HAS_WARNING_PEDANTIC)
#  if(COMPILER_HAS_WARNING_PEDANTIC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9)
#    # gcc 4.8 doesn't support -Wpedantic -Wno-pedantic to selectively disable
#    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=pedantic")
#  endif()
#  # bring the options in sync with the server's
#
#
#  check_cxx_compiler_flag("-Wnon-virtual-dtor" COMPILER_HAS_WARNING_NON_VIRTUAL_DTOR)
#  if(COMPILER_HAS_WARNING_NON_VIRTUAL_DTOR)
#    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wnon-virtual-dtor")
#  endif()
#
#  # GCC/clang have this option
#  # clang doesn't have its sibling: -Wsuggest-attribute=format
#  check_cxx_compiler_flag("-Wmissing-format-attribute" COMPILER_HAS_WARNING_MISSING_FORMAT_ATTRIBUTE)
#  if(COMPILER_HAS_WARNING_MISSING_FORMAT_ATTRIBUTE)
#    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-format-attribute")
#  endif()
#
#  # GCC/clang have this option
#  #
#  # FIXME: as long as protobuf-3.0.0 and gmock 1.7.0 are used, we need to disable -Wundef as it is
#  # triggered all over the place in the headers. It should actually be enabled.
#  check_cxx_compiler_flag("-Wundef" COMPILER_HAS_WARNING_UNDEF)
#  if(COMPILER_HAS_WARNING_UNDEF)
#    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-undef -Wno-conversion")
#  endif()
#
#  # GCC/clang have this option
#  check_cxx_compiler_flag("-Wvla" COMPILER_HAS_WARNING_VLA)
#  if(COMPILER_HAS_WARNING_VLA)
#    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wvla")
#  endif()
#
#  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX14_FLAG}")
#
#else()
#  message(FATAL_ERROR "Compiler ${CMAKE_CXX_COMPILER} is not supported")
#endif()
